package connect

import (
	"project/dao"
	"project/models"
)

func AddStudents(students models.Student) error {
	err := dao.Db.Table("student").Select("sno", "sname", "ssex", "sage", "sdept", "snation", "sregion", "sdegree", "spolitics_status","sdisciplinary_record","smark").Create(&students).Error
	return err
}

func DelStudents(id string) error {
	err := dao.Db.Table("student").Where("sno = ?", id).Delete(models.Student{}).Error
	return err
}

func UpdateStudents(students models.Student) error {
	err := dao.Db.Table("student").Where("sno = ?", students.Sno).Updates(&students).Error
	return err
}

func GetAllStudents(studentsParam map[string]interface{}) (error, []models.Student, int64) {
	var studentsData []models.Student
	page := studentsParam["page"].(int)
	pageSize := studentsParam["limit"].(int)
	searchName := studentsParam["searchName"].(string)
	var total int64
	err := dao.Db.Table("student").Where("sname like ? ", "%"+searchName+"%").Order("sno ASC").Count(&total).Offset((page - 1) * pageSize).Limit(pageSize).Find(&studentsData).Error
	return err, studentsData, total
}
